const Koa = require('koa');
const compose = require('koa-compose');
const fs = require('fs');

const app = new Koa();

const logMiddleware = async function(ctx,next) {
    console.log(1);
    const startTime = Date.now();
    next();

    console.log(4);
    console.log(ctx.method,ctx.url,`spend ${Date.now() - startTime} millisecond`);
} 
const readFileMiddleware = async function(ctx,next) {
    console.log(2);
    ctx.status = 200;
    ctx.body = fs.readFileSync('./index.html');
    ctx.type = 'text/html';
    console.log(3);
} 

app.use(compose([logMiddleware,readFileMiddleware]))

app.listen(1314,() => {
    console.log('服务启动起来了','http://localhost:1314');
})